// src/pieces/AI.cpp
#include "../inc/Chess/AI.h"
#include "../inc/Chess/Pieces.h"
#include "../inc/Chess/advisor.h"
#include "../inc/Chess/elephant.h"
#include "../inc/Chess/general.h"
#include "../inc/Chess/gun.h"
#include "../inc/Chess/horse.h"
#include "../inc/Chess/rook.h"
#include "../inc/Chess/solider.h"
#include <iostream>
#include <algorithm>
#include <cstdlib>
#include <ctime>

using namespace std;

AI::AI(Board* board, bool isRed) : gameBoard(board), isRedSide(isRed)
{
    srand(time(nullptr));
}

bool AI::makeMove()
{
    // 生成所有可能的移动
    auto possibleMoves = generateMoves(isRedSide);
    
    if (possibleMoves.empty()) {
        return false;
    }
    
    // 随机选择一个合法移动
    auto selectedMove = selectRandomMove(possibleMoves);
    
    // 执行移动
    bool result = gameBoard->execute_move(selectedMove.first.first, selectedMove.first.second, 
                                          selectedMove.second.first, selectedMove.second.second);
    
    if (result) {
        gameBoard->show_piece();
    }
    
    return result;
}

vector<pair<pair<int, int>, pair<int, int>>> AI::generateMoves(bool forRed)
{
    vector<pair<pair<int, int>, pair<int, int>>> moves;
    
    // 遍历整个棋盘寻找可能的移动
    for (int row = 0; row < 9; row++) {
        for (int col = 0; col < 10; col++) {
            // 检查当前位置是否有棋子
            if (gameBoard->has_piece(row, col)) {
                // 检查是否是目标方的棋子
                bool isRedPiece = gameBoard->is_red_piece(row, col);
                if (isRedPiece != forRed) continue;
                                
                // 尝试所有可能的目标位置
                for (int to_row = 0; to_row < 9; to_row++) {
                    for (int to_col = 0; to_col < 10; to_col++) {
                        // 获取棋子对象
                        Piece* piece = nullptr;
                        const auto& pieces = gameBoard->get_pieces();
                        for (const auto& p : pieces) {
                            auto pos = p->get_position();
                            if (pos.first == row && pos.second == col) {
                                piece = p.get();
                                break;
                            }
                        }
                        
                         // 创建临时棋盘状态用于验证移动
                        PieceType tempBoard[9][10];
                        gameBoard->clone_board_state(tempBoard);

                        if (piece && piece->is_valid_move(to_row, to_col, tempBoard))
                        {
                            PieceType targetPiece = gameBoard->get_board_piece(to_row, to_col);
                            if (targetPiece != PieceType::kong) {
                                int targetValue = static_cast<int>(targetPiece);
                                if ((forRed && targetValue >= 1 && targetValue <= 16) || 
                                    (!forRed && targetValue >= 17 && targetValue <= 32)) {
                                    // 不能吃掉己方棋子
                                    continue;
                                }
                            }
                            
                            moves.push_back(make_pair(make_pair(row, col), make_pair(to_row, to_col)));
                        }
                    }
                }
            }
        }
    }
    
    return moves;
}

int AI::evaluateBoard()
{
    // 简单的评估函数
    return rand() % 100;
}

pair<pair<int, int>, pair<int, int>> AI::selectRandomMove(
    const vector<pair<pair<int, int>, pair<int, int>>>& moves)
{
    // 随机选择一个移动
    int randomIndex = rand() % moves.size();
    return moves[randomIndex];
}